package cn.iocoder.yudao.module.vote.controller.admin.userref;

import cn.iocoder.yudao.module.vote.controller.admin.voteuserinfo.vo.VoteUserImportExcelVO;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserinfo.vo.VoteUserImportRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.userref.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.userref.VoteUserRefDO;
import cn.iocoder.yudao.module.vote.convert.userref.VoteUserRefConvert;
import cn.iocoder.yudao.module.vote.service.userref.VoteUserRefService;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "管理后台 - 用户投票规则关联")
@RestController
@RequestMapping("/vote/user-ref")
@Validated
public class VoteUserRefController {

    @Resource
    private VoteUserRefService userRefService;

    @PostMapping("/create")
    @ApiOperation("创建用户投票规则关联")
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:create')")
    public CommonResult<Long> createUserRef(@Valid @RequestBody VoteUserRefCreateReqVO createReqVO) {
        return success(userRefService.createUserRef(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户投票规则关联")
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:update')")
    public CommonResult<Boolean> updateUserRef(@Valid @RequestBody VoteUserRefUpdateReqVO updateReqVO) {
        userRefService.updateUserRef(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户投票规则关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:delete')")
    public CommonResult<Boolean> deleteUserRef(@RequestParam("id") Long id) {
        userRefService.deleteUserRef(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户投票规则关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:query')")
    public CommonResult<VoteUserRefRespVO> getUserRef(@RequestParam("id") Long id) {
        VoteUserRefDO userRef = userRefService.getUserRef(id);
        return success(VoteUserRefConvert.INSTANCE.convert(userRef));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户投票规则关联列表")
    @ApiImplicitParam(name = "voteId", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = Integer.class)
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:query')")
    public CommonResult<List<VoteUserRefRespVO>> getUserRefList(@RequestParam("voteId") Integer voteId) {
        List<VoteUserRefDO> list = userRefService.getByVoteId(voteId);
        return success(VoteUserRefConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户投票规则关联分页")
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:query')")
    public CommonResult<PageResult<VoteUserRefRespVO>> getUserRefPage(@Valid VoteUserRefPageReqVO pageVO) {
        PageResult<VoteUserRefDO> pageResult = userRefService.getUserRefPage(pageVO);
        return success(VoteUserRefConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户投票规则关联 Excel")
//    @PreAuthorize("@ss.hasPermission('vote:user-ref:export')")
    @OperateLog(type = EXPORT)
    public void exportUserRefExcel(@Valid VoteUserRefExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VoteUserRefDO> list = userRefService.getUserRefList(exportReqVO);
        // 导出 Excel
        List<VoteUserRefExcelVO> datas = VoteUserRefConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户投票规则关联.xls", "数据", VoteUserRefExcelVO.class, datas);
    }


    @PostMapping("/import")
    @ApiOperation("导入评审用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "updateSupport", value = "是否支持更新，默认为 false", example = "true", dataTypeClass = Boolean.class)
    })
    public CommonResult<VoteUserRefImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                          @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
                                                          @RequestParam(value = "voteId", required = false, defaultValue = "false") Integer voteId

    ) throws Exception {
        List<VoteUserRefImportExcelVO> list = ExcelUtils.read(file, VoteUserRefImportExcelVO.class);
        return success(userRefService.importVoteUsers(list,voteId, updateSupport));
    }


    @GetMapping("/get-import-template")
    @ApiOperation("获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 输出
        ExcelUtils.write(response, "评审导入模板.xls", "评审员列表", VoteUserRefImportExcelVO.class, null);
    }

}
